import router, {
  asyncRoutes,
  constantRoutes
} from '@/router'
// import {
//   getRouters
// } from '@/api/user'
import Layout from '@/layout/index'

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
    console.log(state.routes, "state.routes")
  }
}

const actions = {
  generateRoutes({
    commit
  }) {
    return new Promise(resolve => {
      // getRouters().then(res => {
      const routers = [{
        path: '/zxgl',
        component: 'Layout',
        meta: {
          title: '资讯管理',
          icon: 'example'
        },
        children: [{
          path: '/zxgl',
          component: "",
          meta: {
            title: '资讯管理',
            icon: 'example'
          },
        },
        {
          path: '/zxgl',
          component: "",
          meta: {
            title: '资讯管理',
            icon: 'example'
          },
        },
      
      ],

      }]
      let accessedRoutes = filterAsyncRouter(routers)
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
      // })
    })
  }
}
// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component === 'Layout') { // Layout组件特殊处理
      route.component = Layout
    } else {
      const path = route.component || route.path
      route.component = (resolve) => require([`@/views${path}`], resolve)
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    console.log(route, "route----")
    return route
  })
}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
